import { Router } from 'express';
import { CharacterService } from '../services/character.service.js';
import { AIService } from '../services/ai.service.js';

const router = Router();

// 获取AI提供商状态
router.get('/ai-status', async (req, res) => {
  try {
    const status = AIService.getProviderStatus();
    res.json({
      success: true,
      data: status,
    });
  } catch (error) {
    console.error('获取AI状态失败:', error);
    res.status(500).json({
      success: false,
      error: error.message,
    });
  }
});

// 生成新角色
router.post('/generate', async (req, res) => {
  try {
    const { background, provider } = req.body;

    if (!background) {
      return res.status(400).json({ error: '请提供故事背景' });
    }

    // 使用AI生成角色
    let characterData;
    if (provider) {
      characterData = await AIService.generateCharacterWithProvider(
        background,
        provider
      );
    } else {
      characterData = await AIService.generateCharacter(background);
    }

    // 保存到数据库
    const character = await CharacterService.create({
      name: characterData.name,
      background,
      personality: characterData.personality,
      speakingStyle: characterData.speakingStyle,
      knowledgeBase: characterData.knowledgeBase,
      systemPrompt: characterData.systemPrompt,
    });

    res.json({ success: true, data: character });
  } catch (error) {
    console.error('生成角色失败:', error);
    res.status(500).json({ error: error.message || '生成角色失败' });
  }
});

// 获取所有角色
router.get('/', async (req, res) => {
  try {
    const characters = await CharacterService.findAll();
    res.json({ success: true, data: characters });
  } catch (error) {
    console.error('获取角色列表失败:', error);
    res.status(500).json({ error: '获取角色列表失败' });
  }
});

// 获取单个角色
router.get('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const character = await CharacterService.findById(id);

    if (!character) {
      return res.status(404).json({ error: '角色不存在' });
    }

    res.json({ success: true, data: character });
  } catch (error) {
    console.error('获取角色失败:', error);
    res.status(500).json({ error: '获取角色失败' });
  }
});

// 更新角色
router.put('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const updateData = req.body;

    const character = await CharacterService.update(id, updateData);

    if (!character) {
      return res.status(404).json({ error: '角色不存在' });
    }

    res.json({ success: true, data: character });
  } catch (error) {
    console.error('更新角色失败:', error);
    res.status(500).json({ error: '更新角色失败' });
  }
});

// 删除角色
router.delete('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const success = await CharacterService.delete(id);

    if (!success) {
      return res.status(404).json({ error: '角色不存在' });
    }

    res.json({ success: true, message: '角色删除成功' });
  } catch (error) {
    console.error('删除角色失败:', error);
    res.status(500).json({ error: '删除角色失败' });
  }
});

export default router;
